import shutil
from selenium.webdriver import EdgeOptions, ChromeOptions
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from tools.reader.config_reader import ConfigReader


# def _configure_edge_options():
#     """Configure Edge options."""
#     edge_options = EdgeOptions()
#     edge_options.use_chromium = True
#     edge_config = ConfigReader().read("options")
#
#     window_size = edge_config["window_size"].lower()  # 窗口尺寸
#     headless = edge_config["headless"].lower()  # 无头模式
#     disable_gpu = edge_config["disable_gpu"].lower()  # 禁用GPU加速
#     disable_gpu_program_cache = edge_config["gpu_program_cache"].lower()  # 禁用GPU程序缓存
#     disable_javascript = edge_config["javascript"].lower()  # 禁用运行JavaScript代码
#     enable_automation = edge_config["automation"].lower()  # 允许通过自动测试控制浏览器
#
#     if window_size == "max" or window_size == "MAX":
#         edge_options.add_argument("--start-maximized")
#     elif window_size == "cus" or window_size == "CUS":
#         wide = edge_config["wide"]
#         high = edge_config["high"]
#         edge_options.add_argument("--window-size=" + wide + "," + high)
#     if headless == "true" or headless == "TRUE":
#         edge_options.add_argument("--headless")
#     elif headless == "incognito":
#         edge_options.add_argument("--incognito")
#     if disable_gpu == "true" or disable_gpu == "TRUE":
#         edge_options.add_argument("--disable-gpu")
#     if disable_gpu_program_cache == "true" or disable_gpu_program_cache == "TRUE":
#         edge_options.add_argument("--disable-gpu-program-cache")
#     if disable_javascript == "true" or disable_javascript == "TRUE":
#         edge_options.add_argument("--disable-javascript")
#     if enable_automation == "true" or enable_automation == "TRUE":
#         edge_options.add_argument("--enable-automation")
#     return edge_options
#
#
# def _configure_chrome_options():
#     """Configure Chrome options."""
#     chrome_options = ChromeOptions()
#     chrome_config = ConfigReader().read("options")
#
#     window_size = chrome_config["window_size"].lower()  # 窗口尺寸
#     headless = chrome_config["headless"].lower()  # 无头模式
#     disable_gpu = chrome_config["disable_gpu"].lower()  # 禁用GPU加速
#     disable_gpu_program_cache = chrome_config["gpu_program_cache"].lower()  # 禁用GPU程序缓存
#     disable_javascript = chrome_config["javascript"].lower()  # 禁用运行JavaScript代码
#     enable_automation = chrome_config["automation"].lower()  # 允许通过自动测试控制浏览器
#
#     if window_size == "max" or window_size == "MAX":
#         chrome_options.add_argument("--start-maximized")
#     elif window_size == "cus" or window_size == "CUS":
#         wide = chrome_config["wide"]
#         high = chrome_config["high"]
#         chrome_options.add_argument("--window-size=" + wide + "," + high)
#     if headless == "true" or headless == "TRUE":
#         chrome_options.add_argument("--headless")
#     elif headless == "incognito":
#         chrome_options.add_argument("--incognito")
#     if disable_gpu == "true" or disable_gpu == "TRUE":
#         chrome_options.add_argument("--disable-gpu")
#     if disable_gpu_program_cache == "true" or disable_gpu_program_cache == "TRUE":
#         chrome_options.add_argument("--disable-gpu-program-cache")
#     if disable_javascript == "true" or disable_javascript == "TRUE":
#         chrome_options.add_argument("--disable-javascript")
#     if enable_automation == "true" or enable_automation == "TRUE":
#         chrome_options.add_argument("--enable-automation")
#     return chrome_options


class Assembler:

    def __init__(self):
        self.driver = None
        self.assemble_driver()

    # 驱动装配器
    def assemble_driver(self):
        # 获取配置文件中的driver类型
        driver_type = ConfigReader().read("project")["driver"].lower()
        # 目前只配置了edge浏览器的驱动
        if driver_type == "edge" or driver_type == "EDGE":
            # 配置options
            # edge_options = _configure_edge_options()
            # 配置service
            # self.driver = webdriver.Edge(options=edge_options, service=service)
            pass
        elif driver_type == "chrome" or driver_type == "CHROME":
            # chrome_options = _configure_chrome_options()
            driver_path = ChromeDriverManager().install()
            # new_path = "chromedriver"
            # shutil.copy(driver_path, new_path)
            # self.driver = webdriver.Chrome(service=ChromeService(new_path + 'chromedriver.exe'))
            self.driver = webdriver.Chrome(service=ChromeService(driver_path))
        else:
            self.driver = None
            raise TypeError("Unsupported driver type")

    def disassemble_driver(self):
        """
        Uninstall the driver
        """
        if self.driver is not None:
            self.driver.quit()
            self.driver = None

    def get_driver(self):
        """
        Get the driver
        """
        return self.driver


if __name__ == '__main__':
    # browser_type(bw_type="Chrome", url="https://pypi.org/project/webdriver-manager/")
    assembler = Assembler()
    driver1 = assembler.get_driver()
    driver1.get("https://baidu.com")
